<?php  if ( ! defined('APPPATH')) exit('No direct script access allowed');
/**
 * Mediamarkt Draw
 * @author  shallker.wang@profero.com
 * @date  2012.07.18
 */

function output_success($data=1) {
    die(json_encode($data));
}

function output_failed($mesg='') {
	die(json_encode(array('error'=>$mesg)));
}

function admin_do_login($redirect='') {
    session_start();
    if (! $_SESSION['admin']) {
        header("Location: login.php");
        die();
    }
}

function positive_int($var) {
    return abs(round($var));
}

function paging($items_total, $items_per_page, $page_current) {
    $page_total = ceil($items_total/$items_per_page);
    $page_prev = $page_current - 1;
    $page_next = $page_current + 1;
    if ($page_current <= 1) {
        $page_current = 1;
        $page_prev = 1;
        $page_next = $page_current + 1;
    }
    if ($page_current >= $page_total) {
        $page_current = $page_total;
        $page_prev = $page_current - 1;
        $page_next = $page_total;
    }
    $items_start = ($page_current - 1) * $items_per_page;
    $items_end = $page_current * $items_per_page - 1;
    
    $rt = array();
    $rt['page_total'] = $page_total;
    $rt['page_prev'] = $page_prev;
    $rt['page_current'] = $page_current;
    $rt['page_next'] = $page_next;
    $rt['items_total'] = $items_total;
    $rt['items_start'] = $items_start;
    $rt['items_end'] = $items_end;
    return $rt;
}